package com.identify.identifydrawings;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ImageUpload {

    /**
     * The imgur client ID for OkHttp recipes. If you're using imgur for anything other than running
     * these examples, please request your own client ID! https://api.imgur.com/oauth2
     */
    private static final String IMGUR_CLIENT_ID = "123";
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private static final OkHttpClient client = new OkHttpClient();

    //public  static String imagename=UUID.randomUUID().toString();
    //原是static
    public static void run(File f, final ResponseListener responseListener) throws Exception {
        Log.e("TAG", "run=" + f);
        final File file = f;
        new Thread() {
            @Override
            public void run() {
                //子线程需要做的工作
                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("title", "Square Logo")
                        .addFormDataPart("file", "upload.png",
                                RequestBody.create(MEDIA_TYPE_PNG, file))
                        .build();
                final Request request = new Request.Builder()
                        .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
                        .url("http://192.168.137.1:5000/upload")
                        .post(requestBody)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("TAG", "onFailure: " + e.getMessage());
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("TAG", "response=" + response);
                        String dirName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bbbb";
                        final File file = new File(dirName);
                        if (!file.exists()) {
                            file.mkdir();
                        }
                        File file1 = new File(dirName + "/" + "detect.png");
                        if (response != null) {
                            InputStream is = response.body().byteStream();
                            FileOutputStream fos = new FileOutputStream(file1);
                            int len = 0;
                            byte[] buffer = new byte[2048];
                            while (-1 != (len = is.read(buffer))) {
                                fos.write(buffer, 0, len);
                            }
                            fos.flush();
                            fos.close();
                            is.close();
                        }
                        responseListener.onResonse(file1.getAbsolutePath());
                    }
                });
            }
        }.start();
    }
}


